home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10679 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointers to register
  5. Date: Tue, 19 Mar 1996 13:22:37 +0200
  6. Organization: Carelcomp Products
  7. Message-ID: <314E98FD.5722@cmt.lpr.mail.carel.fi>
  8. References: <1239@altheim.win-uk.net>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Brian R. Oldham wrote:
  16. > A couple of weeks ago someone posted the opinion that all objects in
  17. > memory must have an address, which might have gone uncontested but for
  18. > the fact that he added that therefore all pointers must point to an
  19. > address. Someone else reminded us that registers don't have an address.
  20. > Bearing in mind the usual way to assign a pointer:
  21. >     ptr = &var;
  22. > is correct for objects in memory, but how do you assign a pointer
  23. > to a register?
  24. > The following function (a getch() for x86) works: But is it right??
  25. > /* Read next keystroke */
  26. > #include <dos.h>
  27. > #define KEYBD 0x16
  28. > static union REGS inregs, outregs;
  29. > void getkey(int *scancode, char *ch)
  30. > {
  31. >     inregs.h.ah = 0x00;
  32. >     int86(KEYBD,&inregs,&outregs);
  33. >     *scancode = outregs.h.ah;     /* ??? */
  34. >     *ch = outregs.h.al;
  35. > }
  36.  
  37. You have things a little confused here. The "inregs" and "outregs" used in a call to 
  38. "int86" are structures containing images of the registers. Register, as what your posting 
  39. implies to, are the CPU's internal storages for data, such as AX, BX, SI, etc. In a 
  40. standard C program, you cannot access these directly - the use of them is 
  41. implementation-dependant. However, you can suggest the compiler to use registers by
  42.  
  43.     void    function(void)
  44.     {
  45.         register int    xyz;
  46.         ...
  47.     }
  48.  
  49. or something like that. It still doesn't mean that the compiler will actually place xyz 
  50. in a register - it's just a suggestion you made. Current compilers tend to put variables 
  51. into registers whenever possible (so called automatic vars) to speed things up, but the 
  52. only way to see that is to take look at the compiled code. However, in a situation like 
  53. the next one, the address of a register variable (if it gets to put in a register) is 
  54. never needed, so the compiler may tend to optimize it out (agreed, this is a stupid 
  55. example):
  56.  
  57.     void    function(void)
  58.     {
  59.         register int    xyz;
  60.         int        *p = &xyz;
  61.  
  62.         *p = 0;
  63.     }
  64.  
  65. OTOH, if the compiler does _not_ optimize this out, it will neither put xyz in a 
  66. register, because the address of it is needed.
  67.  
  68. Hope this sheds some light.
  69.  
  70. Later,
  71.  AriL
  72. -- 
  73. All my opinions are mine and mine alone.
  74.